package gui_interface;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.Timer;

public class MovieFrame extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Movie mov;
	String configFile;
	
	//buttons
	int number_of_steps = 0;
	JTextField text;
	JButton go;
	JButton back;
	
	//objects for movie
	JButton movie;
	JButton stopmovie;
	Timer timer;
	int remaining_steps =0;
	int time_between_frames;
	
	//to switch between the two views
	JButton switchGradients;
	
	public MovieFrame(String configFile, String movie, int time)
			throws FileNotFoundException {
		this.configFile = configFile;
		time_between_frames = time;
		setup();
		addMovie(configFile, movie);
		addButtons();

	}

	public void paint(Graphics g) {
		super.paint(g);
	}

	private void setup() {
		Dimension tailleEcran = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		int hauteur = (int) tailleEcran.getHeight();
		int largeur = (int) tailleEcran.getWidth();
		setTitle("Movie");
		setAlwaysOnTop(true);
		setSize(largeur, hauteur);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		getContentPane().setLayout(new GridBagLayout());
	}

	private void addMovie(String configFile, String movie)
			throws FileNotFoundException {
		mov = new Movie(configFile, movie);
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 4;
		c.gridheight = 10;
		c.weightx = 0.5;
		setContentPane(mov);
		
		// getContentPane().add(mov,c);
		// mov.add(10);
		// mov.repaint();
	}

	private void addButtons() {
		go = new JButton("Go");
		go.setActionCommand("go");
		go.addActionListener(this);

		back = new JButton("Back");
		back.setActionCommand("back");
		back.addActionListener(this);

		text = new JTextField(3);
		text.setText("5");

		movie = new JButton("Play Movie");
		movie.setActionCommand("movie");
		movie.addActionListener(this);
		
		stopmovie = new JButton("Stop Movie");
		stopmovie.setActionCommand("stopmovie");
		stopmovie.addActionListener(this);
		
		switchGradients = new JButton("Switch View");
		switchGradients.setActionCommand("switch");
		switchGradients.addActionListener(this);

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 4;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.weightx = 0.5;
		getContentPane().add(go, c);
		c.gridy = 1;
		getContentPane().add(back, c);
		c.gridy = 2;
		getContentPane().add(text, c);
		c.gridy = 3;
		getContentPane().add(movie, c);
		c.gridy = 4;
		getContentPane().add(stopmovie, c);
		c.gridy = 5;
		getContentPane().add(switchGradients, c);
	}

	public void actionPerformed(ActionEvent e) {
		try {
			number_of_steps = Integer.parseInt(text.getText());
		} catch (Exception e1) {
			System.out.println("number of steps must be an integer");
		}
		if (e.getActionCommand().equals("go")) {
			// System.out.println("go");
			mov.add(number_of_steps);
			mov.repaint();
		}
		if (e.getActionCommand().equals("back")) {
			// System.out.println("back");
			mov.remove(number_of_steps);
			mov.repaint();
		}
		if(e.getActionCommand().equals("switch")){
			mov.switchView();
			mov.repaint();
		}
		if (e.getActionCommand().equals("movie")) {
			// System.out.println("back");
			remaining_steps = new Config(configFile).maximal_number_of_nodes;
			timer = new Timer(time_between_frames, this);
			timer.setActionCommand("add1");
		    timer.start();
		}
		if(e.getActionCommand().equals("stopmovie")){
			if (timer.isRunning()){
				timer.stop();
			}
		}
		
		if(e.getActionCommand().equals("add1")){
			if(remaining_steps ==0){
				timer.stop();
			}
			
				mov.add(1);
				mov.repaint();
				remaining_steps--;
			}

		

	}

}
